package com.foreveross.proxyip.web.action.operate;


import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.dayatang.domain.InstanceFactory;
import com.foreveross.proxyip.application.operate.ChannelEntityApplication;
import com.foreveross.proxyip.core.pools.PoolsManager;
import com.foreveross.proxyip.exception.ProxyIpServiceException;
import com.foreveross.proxyip.infra.PageData;
import com.foreveross.proxyip.monitor.vo.ChannelEntityVO;
import com.foreveross.proxyip.web.action.BaseAction;
@SuppressWarnings("serial")
@Controller
@org.springframework.context.annotation.Scope("prototype")
@Namespace("/operate/channelPoolManager")
@ParentPackage("json-default")
public class ChannelPoolManagerAction  extends BaseAction {
	@Autowired
	ChannelEntityApplication channelEntityApplication;
	
	PageData pageData;
	
	private int pageIndex;

	private int pageLen;
	
	private ChannelEntityVO vo = new ChannelEntityVO();
	
	boolean ischannel;
	
	
	@Action(value="toJsp",results={@Result(name="dispatcher", type="dispatcher",location="/operate/channelPoolManager.jsp")})
	public String toJsp()throws ProxyIpServiceException{
		PoolsManager poolsManager = InstanceFactory.getInstance(PoolsManager.class);
		vo.setSpeedGrades(poolsManager.getPublicPool().getValidate().speedGrades) ;
		return "dispatcher";
	}
	
	/**
	 * 查找所有渠道
	 * @return
	 */
	@Action(value="search", results={@Result(name="JSON", type="json", params={"root","pageData"})})
	public String search()throws Exception{
		pageData=channelEntityApplication.findAllChannel( vo,pageIndex, pageLen);
		return "JSON";
	}
	/**
	 * 增加渠道
	 * @return
	 * @throws Exception
	 */
	@Action(value="addChannel", results={@Result(name="JSON", type="json", params={"root","pageData"})})
	public String addChannel()throws Exception{
		//获取当前用户名称
		String createName=((org.springframework.security.core.context.SecurityContextImpl)super.getSession().getAttribute("SPRING_SECURITY_CONTEXT")).getAuthentication().getName();
		vo.setCreateName(createName);
		channelEntityApplication.addChannel(vo);
		return "JSON";
	}
	/**
	 * 修改渠道
	 * @return
	 * @throws Exception
	 */
	@Action(value="updateChannel", results={@Result(name="JSON", type="json", params={"root","pageData"})})
	public String updateChannel()throws Exception{
		//获取当前用户名称
		String createName=((org.springframework.security.core.context.SecurityContextImpl)super.getSession().getAttribute("SPRING_SECURITY_CONTEXT")).getAuthentication().getName();
		vo.setCreateName(createName);
		channelEntityApplication.updateChannel(vo);
		return "JSON";
	}
	/**
	 * 删除渠道
	 * @return
	 * @throws Exception
	 */
	@Action(value="deleteChannel", results={@Result(name="JSON", type="json", params={"root","pageData"})})
	public String deleteChannel()throws Exception{
		channelEntityApplication.deleteChannel(vo);
		pageData=channelEntityApplication.findAllChannel( vo,pageIndex, pageLen);
		return "JSON";
	}
	/**
	 * 停止渠道管理
	 * @return
	 * @throws Exception
	 */
	@Action(value="stopState", results={@Result(name="JSON", type="json", params={"root","pageData"})})
	public String stopState()throws Exception{
		channelEntityApplication.stopChannel(vo);
		return "JSON";
	}
	/**
	 * 启动渠道管理
	 * @return
	 * @throws Exception
	 */
	@Action(value="startState", results={@Result(name="JSON", type="json", params={"root","pageData"})})
	public String startState()throws Exception{
		channelEntityApplication.startChannel(vo);
		
		return "JSON";
	}
	
	/**
	 * 后台验证用户输入的渠道编码是否存在
	 * @return
	 * @throws Exception
	 */
	@Action(value="checkChannel",results={@Result(name="JSON", type="json", params={"root","ischannel"})})
	public String checkChannel()throws Exception{
		ischannel=channelEntityApplication.checkChannel(vo);
		return "JSON";
	}
	
	@Action(value="validatePass",results={@Result(name="JSON", type="json", params={"root","vo.isValid"})})
	public String validatePass(){
		
		return "JSON";
	}
	
	
	
	public PageData getPageData() {
		return pageData;
	}

	public void setPageData(PageData pageData) {
		this.pageData = pageData;
	}

	public int getPageIndex() {
		return pageIndex;
	}

	public void setPageIndex(int pageIndex) {
		this.pageIndex = pageIndex;
	}

	public int getPageLen() {
		return pageLen;
	}

	public void setPageLen(int pageLen) {
		this.pageLen = pageLen;
	}

	public ChannelEntityApplication getChannelEntityApplication() {
		return channelEntityApplication;
	}

	public void setChannelEntityApplication(
			ChannelEntityApplication channelEntityApplication) {
		this.channelEntityApplication = channelEntityApplication;
	}

	public ChannelEntityVO getVo() {
		return vo;
	}

	public void setVo(ChannelEntityVO vo) {
		this.vo = vo;
	}

	public boolean isIschannel() {
		return ischannel;
	}

	public void setIschannel(boolean ischannel) {
		this.ischannel = ischannel;
	}

			
}